<?php

namespace App\Http\Controllers\Front;

use App\Models\AdvContent;
use App\Models\AgricGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ShopController extends Controller
{
    private $data = array();

    public function __construct(Request $request)
    {
        $this->data['pageTitle'] = '乡通-市场';
        $this->data['menuType'] = 'shop';
    }


    /**
     * 市场
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        // 今日推荐
        $adv_goods = AgricGoods::where('is_show', 1)->orderBy('is_top', 'desc')->orderBy('id', 'desc')->take(10)->get();

        // banner
        $banner_adv = AdvContent::getAdvContent(0, 1, 0, 63); // 获得banner广告位

        $this->data['banner_adv'] = !$banner_adv->isEmpty() ? $banner_adv[0] : $banner_adv;
        $this->data['adv_goods'] = $adv_goods;
        return view('front.shop.index', $this->data);
    }


    /**
     * 获得商品列表
     * @param Request $request
     * @return array
     */
    public function ajax_goods_lists(Request $request)
    {
        $pagesize = $request->pagesize ?? 20;
        $goods_list = AgricGoods::where('is_show', 1)
            ->select(['id', 'goods_name', 'goods_img', 'acticity_price', 'goods_price', 'region_id', 'slider_img'])
            ->orderBy('is_top', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($pagesize);

        return ajaxSuccess('获得成功', $goods_list);
    }

    /**
     * 商品详情
     * @param Request $request
     * @param int $goodsId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function details(Request $request, $goodsId = 0)
    {
        $goods = AgricGoods::where('is_show', 1)->with(['seller_info', 'goods_descriptions'])->find($goodsId);
        if (!$goods) {
            return redirect()->back();
        }

        // 商品热销 8条
        $hot_goods = AgricGoods::where('is_show', 1)->where('category_id', $goods->category_id)->where('id', '!=', $goodsId)->orderBy('hit_num', 'desc')->take(8)->get();

        $this->data['menuType'] = '';
        $this->data['positionInfo'] = ['name' => '市场', 'url' => url('shop')];
        $this->data['goods'] = $goods;
        $this->data['hot_goods'] = $hot_goods;
        return view('front.shop.details', $this->data);
    }
}
